<html>
<head><meta charset="utf-8"><title>drop_in_place safety · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html">drop_in_place safety</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201697769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201697769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201697769">(Jun 23 2020 at 08:08)</a>:</h4>
<p>Hi,<br>
the safety docs for <code>std::ptr::drop_in_place</code> say: <br>
<code>Additionally, if T is not Copy, using the pointed-to value after calling drop_in_place can cause undefined behavior</code>.<br>
Only the <em>use</em> of that pointer cause UB or  also just holding a reference to that object?</p>
<p>Something along these lines: <a href="https://play.rust-lang.org/?gist=8adc59a9026c4d6fc85cb68a6f1da2e9">https://play.rust-lang.org/?gist=8adc59a9026c4d6fc85cb68a6f1da2e9</a></p>



<a name="201699377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201699377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201699377">(Jun 23 2020 at 08:26)</a>:</h4>
<p><code>Drop::drop</code> itself takes <code>&amp;mut self</code> so whatever state the object was left in after being destroyed must be a state that is valid according to the Rust language, but not necessarily a state that the object itself supports any further operations on. Therefore holding a reference to a dropped object <em>should</em> be fine as long as you don't attempt to call any methods of that object which make assumptions about the state of the object which were invalidated during the drop.</p>



<a name="201699444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201699444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201699444">(Jun 23 2020 at 08:27)</a>:</h4>
<blockquote>
<p>call any methods of that object which make assumptions about the state of the object which were invalidated during the drop.</p>
</blockquote>
<p>Or any of its fields</p>



<a name="201700753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201700753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201700753">(Jun 23 2020 at 08:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125267">Peter Rabbit</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/drop_in_place.20safety/near/201699377">said</a>:</p>
<blockquote>
<p><code>Drop::drop</code> itself takes <code>&amp;mut self</code> so whatever state the object was left in after being destroyed must be a state that is valid according to the Rust language, but not necessarily a state that the object itself supports any further operations on.</p>
</blockquote>
<p>That's a great point!<br>
I still wonder about other subtleties though, like this: <a href="https://play.rust-lang.org/?gist=d2498c9a727155d188969763b8464077">https://play.rust-lang.org/?gist=d2498c9a727155d188969763b8464077</a> I don't do any operations on the vec afterwards, but I assume the slice is emitting <code>dereferencable</code> to llvm. and it stays after it's not, is this UB? (obviously under any optimization the slice will be entirely removed)</p>



<a name="201701346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201701346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201701346">(Jun 23 2020 at 08:48)</a>:</h4>
<p>When calling <code>drop_in_place</code> on the <code>Vec</code>, the allocation that backed the slice got deallocated, yet you still passed the slice to a function that has a <code>dereferencable</code> attribute for it's reference argument. This is UB.</p>



<a name="201705627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201705627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201705627">(Jun 23 2020 at 09:39)</a>:</h4>
<p>While references to the value itself which was dropped are okay to have but not touch, references to memory which was deallocated by the drop are UB to even have at all.</p>



<a name="201753001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place%20safety/near/201753001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/drop_in_place.20safety.html#201753001">(Jun 23 2020 at 16:59)</a>:</h4>
<p>IOW, using that internal slice here counts as "making assumptions about the state of the object"</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>